package com.inforich.app.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.inforich.app.api.dto.ProjectRubbishExpandDTO;
import com.inforich.app.api.service.IAppProjectRubbishExpandService;
import com.inforich.app.api.service.IAppProjectService;
import com.inforich.app.component.LoginUserApp;
import com.inforich.common.constant.ProjectConstants;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.ApproveEnum;
import com.inforich.common.enums.StageEnum;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.domain.ProjectRubbishExpand;
import com.inforich.projectManage.domain.ProjectRubbishReport;
import com.inforich.projectManage.service.IProjectRubbishDetailService;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import com.inforich.projectManage.service.IProjectService;
import java.util.Objects;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: some desc
 * @author: HJM
 * @date: 2022/10/30
 */
@Service
public class AppProjectRubbishExpandServiceImpl implements IAppProjectRubbishExpandService {

  @Autowired
  private IProjectRubbishExpandService projectRubbishExpandService;
  @Autowired
  private IAppProjectService appProjectService;
  @Autowired
  private IProjectRubbishDetailService projectRubbishDetailService;
  @Autowired
  private IProjectService projectService;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public AjaxResult addProjectExpand(LoginUserApp loginUserApp, ProjectRubbishExpandDTO dto) {
    int count = projectRubbishExpandService.countByProjectId(dto.getProjectId());
    if (count > 0) {
      return AjaxResult.error("不可重复添加新改扩登记");
    }
    Project project = projectService.getProjectById(dto.getProjectId());
    if (ProjectConstants.PROJECT_KIND_REMOVAL.equals(project.getProjectKindId())) {
      return AjaxResult.error("当前项目为拆迁项目，不可进行新扩改登记");
    }
    ProjectRubbishExpand pre = new ProjectRubbishExpand();
    // 可利用重量不能大于预计垃圾重量
    if (dto.getRubbishRecycleWeight().compareTo(dto.getRubbishPlanWeight()) == 1) {
      return AjaxResult.error("可利用重量不能大于预计垃圾重量");
    }
    BeanUtils.copyProperties(dto, pre);
    if ("1".equals(dto.getIsSubmit())) {
      pre.setApprovalStatus(ApproveEnum.REVIEW.getDbData());
    }

    pre.setBaseStage(CollUtil.isNotEmpty(dto.getRubbishBaseList()) ? "1" : "0");
    pre.setMainStage(CollUtil.isNotEmpty(dto.getRubbishMainList()) ? "1" : "0");
    pre.setDecorateStage(CollUtil.isNotEmpty(dto.getRubbishDecorateList()) ? "1" : "0");
    pre.setSubmitStage(CollUtil.isNotEmpty(dto.getRubbishSubmitList()) ? "1" : "0");
    // 保存垃圾明细
    String projectId = dto.getProjectId();
    projectRubbishDetailService.saveDetail(dto.getRubbishBaseList(), projectId,
        StageEnum.BASE_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishMainList(), projectId,
        StageEnum.MAIN_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishDecorateList(), projectId,
        StageEnum.DECORATE_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishSubmitList(), projectId,
        StageEnum.SUBMIT_STAGE);
    return AjaxResult.success(projectRubbishExpandService.save(pre));
  }

  @Override
  public AjaxResult updateProjectExpand(LoginUserApp loginUserApp, ProjectRubbishExpandDTO dto) {
    String id = dto.getId();
    ProjectRubbishExpand pre = projectRubbishExpandService.getProjectRubbishExpandById(id);
    if (Objects.equals(ApproveEnum.REVIEW.getDbData(), pre.getApprovalStatus())) {
      return AjaxResult.error("待审核的新改扩登记不可修改");
    }
    if (Objects.equals(ApproveEnum.PASS.getDbData(), pre.getApprovalStatus())) {
      return AjaxResult.error("已通过的新改扩登记不可修改");
    }
    BeanUtils.copyProperties(dto, pre);
    if ("1".equals(dto.getIsSubmit())) {
      pre.setApprovalStatus(ApproveEnum.REVIEW.getDbData());
    }

    pre.setBaseStage(CollUtil.isNotEmpty(dto.getRubbishBaseList()) ? "1" : "0");
    pre.setMainStage(CollUtil.isNotEmpty(dto.getRubbishMainList()) ? "1" : "0");
    pre.setDecorateStage(CollUtil.isNotEmpty(dto.getRubbishDecorateList()) ? "1" : "0");
    pre.setSubmitStage(CollUtil.isNotEmpty(dto.getRubbishSubmitList()) ? "1" : "0");
    // 保存垃圾明细
    String projectId = dto.getProjectId();
    projectRubbishDetailService.saveDetail(dto.getRubbishBaseList(), projectId,
        StageEnum.BASE_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishMainList(), projectId,
        StageEnum.MAIN_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishDecorateList(), projectId,
        StageEnum.DECORATE_STAGE);
    projectRubbishDetailService.saveDetail(dto.getRubbishSubmitList(), projectId,
        StageEnum.SUBMIT_STAGE);
    return AjaxResult.success(projectRubbishExpandService.updateById(pre));
  }

  @Override
  public AjaxResult myProjectExpand(LoginUserApp loginUserApp) {
    Project project = appProjectService.getMyProject(loginUserApp);
    if (project == null) {
      return AjaxResult.error("请先进行项目认证");
    }
    ProjectRubbishExpand rubbishExpand = projectRubbishExpandService.getProjectRubbishExpandByProjectId(
        project.getId());
    if (rubbishExpand == null) {
      rubbishExpand = new ProjectRubbishExpand();
      rubbishExpand.setProjectId(project.getId());
      rubbishExpand.setProjectName(project.getName());
      rubbishExpand.setProjectAddress(project.getAddress());
      rubbishExpand.setProjectKindId(project.getProjectKindId());
      rubbishExpand.setProjectLocation(project.getLocation());
    }
    return AjaxResult.success(rubbishExpand);
  }
}
